﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.Domain.wdx.ViewModels;
using B.S.DataBase.MyProject.Domain.患者移动端;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.居民档案PC端;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.居民档案PC端
{
    public class GetResidentFilesDetailCommandHandler : IRequestHandler<GetResidentFilesDetailCommand, APIResult<View_ResidentFileDetail>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;

        public GetResidentFilesDetailCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        public Task<APIResult<View_ResidentFileDetail>> Handle(GetResidentFilesDetailCommand request, CancellationToken cancellationToken)
        {
            APIResult<View_ResidentFileDetail> result = new APIResult<View_ResidentFileDetail>();
            var medicalRecordList = sqlSugar.db.Queryable<MedicalRecords>().Where(x => x.Patient_Id == request.ResidentFiles_Id);
            var list = sqlSugar.db.Queryable<ResidentFiles>()
           .LeftJoin<NCDSign>((file, sign) => file.ResidentFiles_Id == sign.Patient_Id)//多个条件用&&
           .Select((file, sign) => new View_ResidentFileDetail
           {
               ResidentFiles_Id = file.ResidentFiles_Id,
               ResidentFiles_Name = file.ResidentFiles_Name,
               Sex = file.Sex == true ? "男" : "女",
               IdCard = file.IdCard,
               Age = file.Age,
               Address = file.Address,
               Phone = file.Phone,
               Birthday = file.Birthday.DateTimeToString("yyyy-MM-dd HH:mm:ss", "--"),
               CreateTime = file.CreateTime,
               Sign_Status = sign.Sign_Status,
               Doctor = sign.Doctor,
               Team = sign.Team,
               Marriage = file.Marriage,
               BloodType = file.BloodType,
               InsuredSituation = file.InsuredSituation,
               job = file.job,
               Education = file.Education,
               IfSmoke = file.IfSmoke,
               IfQuitSmoke = file.IfQuitSmoke,
               IfPassiveSmoke = file.IfPassiveSmoke,
               IfDrinkWine = file.IfDrinkWine,
               IfSport = file.IfSport,
               Hypertension = file.Hypertension,
               Diabetes = file.Diabetes,
               Height = file.Height,
               Weight = file.Weight,
               BMI = file.BMI,
               Waistline = file.Waistline,
               SystolicPressure = file.SystolicPressure,
               SiastolicPressure = file.SiastolicPressure,
               FastingBloodGlucose = file.FastingBloodGlucose,
               CreateUser = file.CreateUser,
               UpdateUser = file.UpdateUser,
               UpdateTime = file.UpdateTime,
               Sign_StartTime = sign.Sign_StartTime,
               Sign_EndTime = sign.Sign_EndTime,
               medicalRecords = medicalRecordList.ToList(),
           });
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            return Task.FromResult(result);
        }
    }
}
